package com.taobao.ued.toy.web;

import com.taobao.cms.biz.ao.PhotoAO;
import com.taobao.session.SessionKeyConstants;
import com.taobao.ued.toy.domain.Designer;
import com.taobao.ued.toy.domain.ToyConfig;
import com.taobao.ued.toy.domain.Work;
import com.taobao.ued.toy.service.ToyManager;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class WorkController {
	@Autowired
	private ToyManager toyManager;

	@Resource
	private ToyConfig toyConfig;

	@Resource
	private PhotoAO photoAO;

	@RequestMapping(value = "/secure/works/doAdd", method = RequestMethod.POST)
	public void doAddWork(
			@ModelAttribute Work w,
			HttpServletResponse response,
			@RequestParam MultipartFile picFile,
			HttpSession session,
			HttpServletRequest request
	) throws Exception {
		String id = (String) session.getAttribute(SessionKeyConstants.ATTRIBUTE_USER_ID);

		if (id == null) {
			response.sendRedirect(toyConfig.getLoginUrl() + URLEncoder.encode("http://" + request.getServerName() +
					request.getContextPath() + "/secure/works/add"));
			return;
		}

		Designer d = toyManager.getDesigner(id);

		if (picFile.getSize() > 0) {
			String json = photoAO.upladFile(d.getName(),
					picFile.getOriginalFilename(), picFile.getContentType(),
					picFile.getBytes());
			JSONObject jsonObject = JSONObject.fromObject(json);
			w.setPic((String) jsonObject.get("url"));
		}

		w.setDesignerId(id);
		w.setId(toyManager.addWork(w));
		response.sendRedirect(request.getContextPath() + "/secure/"
				+ w.getId() + "/pics/add");
	}

	@RequestMapping(value = "/secure/works/add", method = RequestMethod.GET)
	public String addWork(HttpServletRequest request,
						  Model model
	) throws Exception {
		model.addAttribute("contextpath", request.getContextPath());
		return "work_upload";
	}

	@RequestMapping(value = "/{domain}/works", method = RequestMethod.GET)
	public String getWorks(@PathVariable String domain,
						   HttpServletRequest request,
						   Model model
	) throws Exception {
		Designer d = toyManager.getDesignerByDomain(domain);
		int page = 0;
		List<Work> covers = toyManager.getCoverWorksByDesigner(d.getId());
		List<Work> works = toyManager.getWorksByDesigner(d.getId(),
				page * toyConfig.getWorkPageCount(),
				(page + 1) * toyConfig.getWorkPageCount());
		int totalCount = toyManager.getWorksByDesignerCount(d.getId());
		int totalPage = totalCount / toyConfig.getWorkPageCount();
		if (totalPage % toyConfig.getWorkPageCount() != 0) {
			totalPage++;
		}
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("works", works);
		model.addAttribute("covers", covers);
		model.addAttribute("designer", d);
		model.addAttribute("contextpath", request.getContextPath());
		return "personal_works";
	}


	@RequestMapping(value = "/{domain}/commonWorks", method = RequestMethod.GET)
	public String getWorks(@PathVariable String domain,
						   @RequestParam(value = "page", required = false) Integer pageObj,
						   Model model
	) throws Exception {
		Designer d = toyManager.getDesignerByDomain(domain);
		int page = 0;
		if (pageObj != null) {
			page = pageObj - 1;
		}
		List<Work> works = toyManager.getWorksByDesigner(d.getId(),
				page * toyConfig.getWorkPageCount(),
				(page + 1) * toyConfig.getWorkPageCount());
		int totalCount = toyManager.getWorksByDesignerCount(d.getId());
		int totalPage = totalCount / toyConfig.getWorkPageCount();
		if (totalPage % toyConfig.getWorkPageCount() != 0) {
			totalPage++;
		}
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("works", works);
		return "personal_works";
	}

}
